﻿using System.IO;
using SharpCompress.Archives;
using SharpCompress.Readers;

namespace ZilLion.Core.Unities.UnitiesMethods
{
    /// <summary>
    ///     文件压缩帮助类库
    /// </summary>
    public class CompressHelper
    {
        /// <summary>
        ///     通用解压 支持rar,zip
        /// </summary>
        /// <param name="compressfilepath"></param>
        /// <param name="uncompressdir"></param>
        public static void UnCompress(string compressfilepath, string uncompressdir)
        {
            var extension = Path.GetExtension(compressfilepath);
            if (extension != null)
            {
                var ext = extension.ToLower();
                if (ext == ".rar")
                    UnRar(compressfilepath, uncompressdir);
                else if (ext == ".zip")
                    UnZip(compressfilepath, uncompressdir);
            }
        }

        /// <summary>
        ///     解压rar
        /// </summary>
        /// <param name="compressfilepath"></param>
        /// <param name="uncompressdir"></param>
        public static void UnRar(string compressfilepath, string uncompressdir)
        {
            using (Stream stream = File.OpenRead(compressfilepath))
            {
                using (var reader = ReaderFactory.Open(stream))
                {
                    while (reader.MoveToNextEntry())
                        if (!reader.Entry.IsDirectory)
                        {
                            var extractionOptions = new ExtractionOptions
                            {
                                ExtractFullPath = true,
                                Overwrite = true
                            };
                            reader.WriteEntryToDirectory(uncompressdir,
                                extractionOptions);
                        }
                }
            }
        }

        /// <summary>
        ///     解压zip
        /// </summary>
        /// <param name="compressfilepath"></param>
        /// <param name="uncompressdir"></param>
        public static void UnZip(string compressfilepath, string uncompressdir)
        {
            using (var archive = ArchiveFactory.Open(compressfilepath))
            {
                foreach (var entry in archive.Entries)
                    if (!entry.IsDirectory)
                    {
                        var extractionOptions = new ExtractionOptions
                        {
                            ExtractFullPath = true,
                            Overwrite = true
                        };
                        entry.WriteToDirectory(uncompressdir,
                            extractionOptions);
                    }
            }
        }
    }
}